//
// Created by REEJI2219 on 2024-08-12.
//
#include "Stack.h"
Stack::Stack() {
    max_size=10;
    top=0;
    base=0;
    array = new ElemType[max_size];
}
Stack::~Stack() {
    delete[] array;
}
bool Stack::push(ElemType elem) {
    if(top-base==max_size) {
        //扩容，对原来数据进行复制,扩容失败，则返回false
        return false;
    }
    *(array+top)=elem;
    top++;
    return true;
}
bool Stack::length(int *length) {
    *length = *&top;
}
bool Stack::pop(ElemType *elem) {
    if(top<=0) {
        return false;
    }
    *elem = array[top-1];
    top--;
    return true;
}
bool Stack::isEmpty() {
    return base==top;
}

bool Stack::pirntStatck() {
    for (int i=0;i<top;i++) {
        printf("%d ",array[i]);
    }
    printf("\n");
}
void move(int n,char source,char target) {
    printf("move{%d} {%c}->{%c} \n",n,source,target);
}
/**
 *
 * @param n 多少个
 * @param a 源塔
 * @param b 辅助塔
 * @param c 目标塔
 * @return
 */
int hminio(int n,char a,char b,char c) {
    if(n==1) {
        move(1,a,c);
    }else {
        //从a移动一个小塔到b,c为辅助
        hminio(n-1,a,c,b);
        move(n,a,c);
        //从b移动一个小塔到a，c为辅助
        hminio(n-1,b,a,c);
    }
}
void testHminio() {
    hminio(4,'a','b','c');
}
/**
 * numner = (numbenr/model)*model + numbenr%model
 * @param numner
 * @param model
 */
void convetion(int numner,int model) {
    Stack *stack = new Stack();
    while (numner) {
        stack->push(numner%model);
        numner = numner/model;
    }
    while (!stack->isEmpty()) {
        int n;
        stack->pop(&n);
        printf("%d",n);
    }
}

bool match(int arr[], int length) {
    Stack *stack = new Stack();
    for (int i = 0; i < length; i++) {
        int ele = arr[i];
        switch (ele) {
            case 1: case 2: case 3:
                stack->push(ele);
            break;
            case 4: {
                int before;
                if (!stack->pop(&before) || before != 3) {
                    delete stack;  // Release memory before returning
                    return false;
                }
            } break;
            case 5: {
                int before;
                if (!stack->pop(&before) || before != 2) {
                    delete stack;  // Release memory before returning
                    return false;
                }
            } break;
            case 6: {
                int before;
                if (!stack->pop(&before) || before != 1) {
                    delete stack;  // Release memory before returning
                    return false;
                }
            } break;
            default:
                break;
        }
    }
    bool result = stack->isEmpty();
    delete stack;  // Ensure memory is released
    return result;
}

// int main() {
//     // convetion(1348,8);
//     int arr[] = {1, 2, 3, 4, 5, 6};
//     int length = sizeof(arr) / sizeof(arr[0]);
//     std::cout << "匹配结果: " << match(arr, length) << std::endl;
//     return 0;
// }





